package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.db.domain.system.MpClient;
import com.ruoyi.db.domain.system.PcGoldConfig;
import com.ruoyi.db.mapper.system.MpClientMapper;
import com.ruoyi.db.mapper.system.PcGoldConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IPcGoldConfigService;

import javax.transaction.Transactional;

/**
 * 金藻系统配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-26
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class PcGoldConfigServiceImpl implements IPcGoldConfigService 
{
    @Autowired
    private PcGoldConfigMapper pcGoldConfigMapper;
    @Autowired
    private MpClientMapper mpClientMapper;

    /**
     * 查询金藻系统配置
     * 
     * @param id 金藻系统配置主键
     * @return 金藻系统配置
     */
    @Override
    public PcGoldConfig selectPcGoldConfigById(Long id)
    {
        return pcGoldConfigMapper.selectPcGoldConfigById(id);
    }

    /**
     * 查询金藻系统配置列表
     * 
     * @param pcGoldConfig 金藻系统配置
     * @return 金藻系统配置
     */
    @Override
    public List<PcGoldConfig> selectPcGoldConfigList(PcGoldConfig pcGoldConfig)
    {
        List<PcGoldConfig> list = pcGoldConfigMapper.selectPcGoldConfigList(pcGoldConfig);
        for (PcGoldConfig config: list) {
            config.setCashRate(config.getCashRate().multiply(new BigDecimal("100")));
            config.setTransferRate(config.getTransferRate().multiply(new BigDecimal("100")));
            config.setProductRate(config.getProductRate().multiply(new BigDecimal("100")));
            config.setTaxRate(config.getTaxRate().multiply(new BigDecimal("100")));
        }
        return list;
    }

    /**
     * 新增金藻系统配置
     * 
     * @param pcGoldConfig 金藻系统配置
     * @return 结果
     */
    @Override
    public int insertPcGoldConfig(PcGoldConfig pcGoldConfig)
    {
        return pcGoldConfigMapper.insertPcGoldConfig(pcGoldConfig);
    }

    /**
     * 修改金藻系统配置
     * 
     * @param pcGoldConfig 金藻系统配置
     * @return 结果
     */
    @Override
    public int updatePcGoldConfig(PcGoldConfig pcGoldConfig)
    {
        pcGoldConfig.setProductRate(pcGoldConfig.getProductRate().divide(new BigDecimal(100)));
        pcGoldConfig.setCashRate(pcGoldConfig.getCashRate().divide(new BigDecimal(100)));
        pcGoldConfig.setTransferRate(pcGoldConfig.getTransferRate().divide(new BigDecimal(100)));
        pcGoldConfig.setTaxRate(pcGoldConfig.getTaxRate().divide(new BigDecimal("100")));

        List<MpClient> clients = mpClientMapper.selectMpClientList(new MpClient());
        for (MpClient client:clients) {
            MpClient client1 = new MpClient();
            client1.setClientId(client.getClientId());
            client1.setGoldTransferLimit(pcGoldConfig.getTransferLimit());
            mpClientMapper.updateMpClient(client1);
        }
        return pcGoldConfigMapper.updatePcGoldConfig(pcGoldConfig);
    }

    /**
     * 批量删除金藻系统配置
     * 
     * @param ids 需要删除的金藻系统配置主键
     * @return 结果
     */
    @Override
    public int deletePcGoldConfigByIds(Long[] ids)
    {
        return pcGoldConfigMapper.deletePcGoldConfigByIds(ids);
    }

    /**
     * 删除金藻系统配置信息
     * 
     * @param id 金藻系统配置主键
     * @return 结果
     */
    @Override
    public int deletePcGoldConfigById(Long id)
    {
        return pcGoldConfigMapper.deletePcGoldConfigById(id);
    }
}
